package org.example.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.mapper.DRolePowerMapper;

import org.example.pojo.DUser;
import org.example.pojo.select.DUserSelect;
import org.example.service.DUserService;
import org.example.mapper.DUserMapper;
import org.example.utils.JwtHelper;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author chenhongkai
* @description 针对表【d_user(登录用户表)】的数据库操作Service实现
* @createDate 2024-05-29 13:51:43
*/
@Service
public class DUserServiceImpl extends ServiceImpl<DUserMapper, DUser>
    implements DUserService {

    @Autowired
    private DUserMapper dUserMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private DRolePowerMapper dRolePowerMapper;
    @Override
    public Result<Map> Login(DUser dUser) {
        LambdaQueryWrapper<DUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DUser::getUsername, dUser.getUsername());
        DUser dUserSelect = dUserMapper.selectOne(lambdaQueryWrapper);

        if (dUserSelect == null)
            return Result.warning(501, "用户不存在");
        if(dUserSelect.getUserStatus() == 0)
            return Result.warning(503, "该用户被禁用");
        if (!Objects.equals(dUserSelect.getPassword(), dUser.getPassword()))
            return Result.warning(502, "密码不正确");

        //利用用户id生成token
        String token = jwtHelper.createToken(Long.valueOf(dUserSelect.getUid()));
        Map tokenMap = new HashMap();
        tokenMap.put("token", token);

        return Result.ok(tokenMap);
    }

    public Result<DUser> GetUserInfo(String token) {
        //判断是否有token
        if (StringUtils.isEmpty(token)
                ||
                jwtHelper.isExpiration(token)) {
            return Result.warning(501, "token过期或不存在");
        }
        int userId = jwtHelper.getUserId(token).intValue();

        DUser userSelect = new DUser();
        userSelect.setUid(userId);
        DUser user = dUserMapper.selectByUid(userSelect);

        if(user.getUserStatus() == 0){
            return Result.warning(502,"该用户已经被禁用");
        }

        //获取登录用户的权限
        Integer[] powersId = dRolePowerMapper.selectPowerId(user.getRoleId());
        user.setPowersId(powersId);
        return Result.ok(user);
    }

    @Override
    public Result<Map> selectAll(DUserSelect dUserSelect) {

        Page<DUser> page = new Page<>(dUserSelect.getCurrentPage(),dUserSelect.getPageSize());
        IPage<Map> mapIPage = dUserMapper.SelectAll(page,dUserSelect);

        Map UserMap = new HashMap<>();
        UserMap.put("pageSize",mapIPage.getSize());
        UserMap.put("pageTotal",mapIPage.getTotal());
        UserMap.put("userData",mapIPage.getRecords());

        return Result.ok(UserMap);
    }

    @Override
    public Result<DUser> UpdateStatus(DUser dUser) {
        int row = dUserMapper.updateById(dUser);
        if(row == 0 ) return Result.error("修改用户状态失败");
        return Result.ok(null);
    }

    @Override
    public Result<DUser> Delete(DUser dUser) {
        int row = dUserMapper.deleteById(dUser);
        if(row == 0) return Result.error("删除用户失败");
        return Result.ok(null);
    }

    @Override
    public Result<DUser> UpdatePassword(DUser dUser) {
        int row = dUserMapper.updateById(dUser);
        if(row == 0) return Result.error("修改用户密码失败");
        return Result.ok(null);
    }

    @Override
    public Result<DUser> SelectOne(DUser dUser) {
        DUser selectUser = dUserMapper.selectByUid(dUser);
        if(selectUser == null) return Result.error("没有该用户");
        return Result.ok(selectUser);
    }

    @Override
    public Result<DUser> UpdateUserInfo(DUserSelect dUserSelect) {
        LambdaQueryWrapper<DUser> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(DUser::getUid,dUserSelect.getUid());
        DUser selectUser1 = dUserMapper.selectOne(userLambdaQueryWrapper);

        //是否改用户名
        if(!Objects.equals(dUserSelect.getUsername(), selectUser1.getUsername())){
            //是否重复用户名
            LambdaQueryWrapper<DUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DUser::getUsername,dUserSelect.getUsername());
            DUser selectUser2 = dUserMapper.selectOne(lambdaQueryWrapper);
            if(selectUser2 != null)
                return Result.warning(501,"用户名已经存在");
        }

        DUser dUser = new DUser();
        dUser.setUpdateDate(new Date());
        dUser.setUid(dUserSelect.getUid());
        dUser.setUsername(dUserSelect.getUsername());
        dUser.setPhoneNumber(dUserSelect.getPhoneNumber());
        dUser.setRoleId(dUserSelect.getRoleId());
        dUser.setUserStatus(dUserSelect.getUserStatus());
        int row = dUserMapper.updateById(dUser);
        if(row == 0) return Result.error("修改用户账户信息失败");
        return Result.ok(null);
    }

    @Override
    public Result<DUser> AddUser(DUser dUser) {
        LambdaQueryWrapper<DUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DUser::getUsername,dUser.getUsername());
        DUser selectDUser = dUserMapper.selectOne(lambdaQueryWrapper);
        if(selectDUser != null){
            return Result.warning(501,"该用户已经存在");
        }
        int row = dUserMapper.insert(dUser);
        if(row == 0) return Result.error("新增用户账户失败");
        return Result.ok(null);
    }
}




